/*********************
* Author:Dzlua
* QQ:505544956
* time:2016/12/02
*/
#include "LuaEdit.h"
#include "../../utilities/Log.h"

#ifdef EDUI_USE_LUAPLUS

EDUI_NAMESPACE_BEGIN

#define CObjPtr (Edit::ToEdit(m_cobj))

void* LuaEdit::GetInterface(void* data)
{
    if (!data) return 0;
    LuaEdit* edit = new LuaEdit((Edit*)data);
#ifdef EDUI_TEST_NEW_DELETE
    ++m_newdeleteNum;
    Log("[LuaEdit] new LuaEdit : %d, %d", edit, data);
#endif
    return edit;
}

void LuaEdit::Release()
{
#ifdef EDUI_TEST_NEW_DELETE
    --m_newdeleteNum;
    Log("[LuaEdit] delete LuaEdit : %d, %d, %s", this, m_cobj, this->GetName());
#endif
    delete this;
}

LuaEdit::LuaEdit(Edit* edit) : LuaLabel(edit)
{}

LuaEdit::~LuaEdit()
{}

//----------------------------------------//

int LuaEdit::GetTipFont(LuaState *L)
{
    LuaStack args(L);
    //cause RegisterObjectFunctor,the first param must be args[2]
    bool bWindow = true;
    if (args[2].IsBoolean()) bWindow = args[2].GetBoolean();
    IFont* font = CObjPtr->GetTipFont(bWindow);
    LuaObject obj = LuaHelperNS::GetFromObjs(L, font);
    obj.Push(L);
    return 1;
}

int LuaEdit::SetTipFont(LuaState *L)
{
    LuaStack args(L);
    bool bRet = false;
    //cause RegisterObjectFunctor,the first param must be args[2]
    IFont* font = (IFont*) LuaHelperNS::GetCObjectData(args[2]);
    bRet = CObjPtr->SetTipFont(font);
    L->PushBoolean(bRet);
    return 1;
}

void LuaEdit::SetText(const char* text)
{
    CObjPtr->SetText(text);
}

int LuaEdit::ReplaceText(LuaState *L)
{
    LuaStack args(L);
    //cause RegisterObjectFunctor,the first param must be args[2]
    bool bRet = false;
    if (args[2].IsString() && args[3].IsInteger() && args[4].IsInteger()){
        bool paste = true;
        if (args[5].IsBoolean()) paste = args[5].GetBoolean();
        bRet = CObjPtr->ReplaceText(args[2].GetString(), args[3].GetInteger(), args[4].GetInteger(), paste);
    }
    L->PushBoolean(bRet);
    return 1;
}

bool LuaEdit::DeleteText(int begin, int end)
{
    return CObjPtr->DeleteText(begin, end);
}

void LuaEdit::ClearText()
{
    CObjPtr->ClearText();
}

int LuaEdit::GetText(LuaState *L)
{
    LuaStack args(L);
    //cause RegisterObjectFunctor,the first param must be args[2]
    if (args[2].IsInteger() && args[3].IsInteger()){
        String str = CObjPtr->GetText(args[2].GetInteger(), args[3].GetInteger());
        L->PushString(str.GetString());
    } else {
        L->PushString(CObjPtr->GetText());
    }
    return 1;
}

void LuaEdit::SelAllText()
{
    CObjPtr->SelAllText();
}

void LuaEdit::SelText(int begin, int end)
{
    CObjPtr->SelText(begin, end);
}

int LuaEdit::ReplaceSel(LuaState *L)
{
    LuaStack args(L);
    //cause RegisterObjectFunctor,the first param must be args[2]
    if (args[2].IsString()){
        bool paste = true;
        if (args[3].IsBoolean()) paste = args[3].GetBoolean();
        CObjPtr->ReplaceSel(args[2].GetString(), paste);
    }
    return 0;
}

void LuaEdit::DeleteSel()
{
    CObjPtr->DeleteSel();
}

int LuaEdit::GetSel(LuaState *L)
{
    int b, e;
    bool bRet = CObjPtr->GetSel(&b, &e);
    if (!bRet) L->PushNil();
    else {
        LuaObject obj;
        obj.SetInteger("b", b);
        obj.SetInteger("e", e);
        obj.Push(L);
    }
    return 1;
}

int LuaEdit::GetSelText(LuaState *L)
{
    String str = CObjPtr->GetSelText();
    L->PushString(str.GetString());
    return 1;
}

int LuaEdit::GetSelPos(LuaState *L)
{
    int b, e;
    bool bRet = CObjPtr->GetSelPos(&b, &e);
    if (!bRet) L->PushNil();
    else {
        LuaObject obj;
        obj.SetInteger("b", b);
        obj.SetInteger("e", e);
        obj.Push(L);
    }
    return 1;
}

void LuaEdit::DeletePrev()
{
    CObjPtr->DeletePrev();
}

void LuaEdit::DeleteNext()
{
    CObjPtr->DeleteNext();
}

void LuaEdit::Copy()
{
    CObjPtr->Copy();
}

void LuaEdit::Cut()
{
    CObjPtr->Cut();
}

void LuaEdit::Paste()
{
    CObjPtr->Paste();
}

void LuaEdit::Delete()
{
    CObjPtr->Delete();
}

void LuaEdit::BackSpace()
{
    CObjPtr->BackSpace();
}

void LuaEdit::SetCursorNextPos()
{
    CObjPtr->SetCursorNextPos();
}

void LuaEdit::SetCursorPrevPos()
{
    CObjPtr->SetCursorPrevPos();
}

void LuaEdit::SetCursorPos(int pos)
{
    CObjPtr->SetCursorPos(pos);
}

int LuaEdit::GetCursorPos()
{
    return CObjPtr->GetCursorPos();
}

void LuaEdit::EnableCursor(bool bEnable)
{
    CObjPtr->EnableCursor(bEnable);
}

void LuaEdit::EnableSel(bool bEnable)
{
    CObjPtr->EnableSel(bEnable);
}

void LuaEdit::ReadOnly(bool bOnlyRead)
{
    CObjPtr->ReadOnly(bOnlyRead);
}

void LuaEdit::SetMaxChar(int maxchar)
{
    CObjPtr->SetMaxChar(maxchar);
}

void LuaEdit::SetTipText(const char* text)
{
    CObjPtr->SetTipText(text);
}

const char* LuaEdit::GetTipText()
{
    return CObjPtr->GetTipText();
}

void LuaEdit::SetReadOnly(bool readonly)
{
    CObjPtr->SetReadOnly(readonly);
}

bool LuaEdit::IsReadOnly()
{
    return CObjPtr->IsReadOnly();
}

void LuaEdit::SetUsePassWord(bool use)
{
    CObjPtr->SetUsePassWord(use);
}

bool LuaEdit::IsUsedPassWord()
{
    return CObjPtr->IsUsedPassWord();
}

const char* LuaEdit::GetPassWordText()
{
    return CObjPtr->GetPassWordText();
}

void LuaEdit::SetPassWordMask(const char* mask)
{
    CObjPtr->SetPassWordMask(mask);
}

const char* LuaEdit::GetPassWordMask()
{
    return CObjPtr->GetPassWordMask();
}

void LuaEdit::SetInputRegex(const char* regex)
{
    CObjPtr->SetInputRegex(regex);
}

void LuaEdit::SetVerifyRegex(const char* regex)
{
    CObjPtr->SetVerifyRegex(regex);
}

int LuaEdit::Verify(LuaState *L)
{
    LuaStack args(L);
    //cause RegisterObjectFunctor,the first param must be args[2]
    bool bRet = false;
    if (args[2].IsString()){
        bRet = CObjPtr->Verify(args[2].GetString());
    } else {
        bRet = CObjPtr->Verify();
    }
    L->PushBoolean(bRet);
    return 1;
}

//----------------------------------------//

void LuaEdit::Register(LuaState *L)
{
    LuaObject obj = LuaHelperNS::AddToMetas(L, "Edit", "Label");
    obj.SetObject("__index", obj);
    obj.RegisterDirect("__NewCObj__", &LuaEdit::GetInterface);

    obj.RegisterObjectFunctor("GetTipFont", &LuaEdit::GetTipFont);
    obj.RegisterObjectFunctor("SetTipFont", &LuaEdit::SetTipFont);
    obj.RegisterObjectDirect("SetText", (LuaEdit*)0, &LuaEdit::SetText);
    obj.RegisterObjectFunctor("ReplaceText", &LuaEdit::ReplaceText);
    obj.RegisterObjectDirect("DeleteText", (LuaEdit*)0, &LuaEdit::DeleteText);
    obj.RegisterObjectDirect("ClearText", (LuaEdit*)0, &LuaEdit::ClearText);
    obj.RegisterObjectFunctor("GetText", &LuaEdit::GetText);
    obj.RegisterObjectDirect("SelAllText", (LuaEdit*)0, &LuaEdit::SelAllText);
    obj.RegisterObjectDirect("SelText", (LuaEdit*)0, &LuaEdit::SelText);
    obj.RegisterObjectFunctor("ReplaceSel", &LuaEdit::ReplaceSel);
    obj.RegisterObjectDirect("DeleteSel", (LuaEdit*)0, &LuaEdit::DeleteSel);
    obj.RegisterObjectFunctor("GetSel", &LuaEdit::GetSel);
    obj.RegisterObjectFunctor("GetSelText", &LuaEdit::GetSelText);
    obj.RegisterObjectFunctor("GetSelPos", &LuaEdit::GetSelPos);
    obj.RegisterObjectDirect("DeletePrev", (LuaEdit*)0, &LuaEdit::DeletePrev);
    obj.RegisterObjectDirect("DeleteNext", (LuaEdit*)0, &LuaEdit::DeleteNext);
    obj.RegisterObjectDirect("Copy", (LuaEdit*)0, &LuaEdit::Copy);
    obj.RegisterObjectDirect("Cut", (LuaEdit*)0, &LuaEdit::Cut);
    obj.RegisterObjectDirect("Paste", (LuaEdit*)0, &LuaEdit::Paste);
    obj.RegisterObjectDirect("Delete", (LuaEdit*)0, &LuaEdit::Delete);
    obj.RegisterObjectDirect("BackSpace", (LuaEdit*)0, &LuaEdit::BackSpace);
    obj.RegisterObjectDirect("SetCursorNextPos", (LuaEdit*)0, &LuaEdit::SetCursorNextPos);
    obj.RegisterObjectDirect("SetCursorPrevPos", (LuaEdit*)0, &LuaEdit::SetCursorPrevPos);
    obj.RegisterObjectDirect("SetCursorPos", (LuaEdit*)0, &LuaEdit::SetCursorPos);
    obj.RegisterObjectDirect("GetCursorPos", (LuaEdit*)0, &LuaEdit::GetCursorPos);
    obj.RegisterObjectDirect("EnableCursor", (LuaEdit*)0, &LuaEdit::EnableCursor);
    obj.RegisterObjectDirect("EnableSel", (LuaEdit*)0, &LuaEdit::EnableSel);
    obj.RegisterObjectDirect("ReadOnly", (LuaEdit*)0, &LuaEdit::ReadOnly);
    obj.RegisterObjectDirect("SetMaxChar", (LuaEdit*)0, &LuaEdit::SetMaxChar);
    obj.RegisterObjectDirect("SetTipText", (LuaEdit*)0, &LuaEdit::SetTipText);
    obj.RegisterObjectDirect("GetTipText", (LuaEdit*)0, &LuaEdit::GetTipText);
    obj.RegisterObjectDirect("SetReadOnly", (LuaEdit*)0, &LuaEdit::SetReadOnly);
    obj.RegisterObjectDirect("IsReadOnly", (LuaEdit*)0, &LuaEdit::IsReadOnly);
    obj.RegisterObjectDirect("SetUsePassWord", (LuaEdit*)0, &LuaEdit::SetUsePassWord);
    obj.RegisterObjectDirect("IsUsedPassWord", (LuaEdit*)0, &LuaEdit::IsUsedPassWord);
    obj.RegisterObjectDirect("GetPassWordText", (LuaEdit*)0, &LuaEdit::GetPassWordText);
    obj.RegisterObjectDirect("SetPassWordMask", (LuaEdit*)0, &LuaEdit::SetPassWordMask);
    obj.RegisterObjectDirect("GetPassWordMask", (LuaEdit*)0, &LuaEdit::GetPassWordMask);
    obj.RegisterObjectDirect("SetInputRegex", (LuaEdit*)0, &LuaEdit::SetInputRegex);
    obj.RegisterObjectDirect("SetVerifyRegex", (LuaEdit*)0, &LuaEdit::SetVerifyRegex);
    obj.RegisterObjectFunctor("Verify", &LuaEdit::Verify);
}

#undef CObjPtr

EDUI_NAMESPACE_END

#endif